<?php
/**
*
* @package phpBB3
* @version $Id: print_post.php,v 1.0.2 2008/12/30 18:39:48 rmcgirr83 Exp $
* @copyright (c) Rich McGirr
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);


$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup(array('mods/print_post','viewtopic'));

$print_post = request_var('p', 0);
$print_post_forum = request_var('f', 0);

// is there a forum id?
if (!$print_post_forum)
{
	trigger_error('NO_FORUM');
}

// is there a post id?
if(!$print_post)
{
	trigger_error ('NO_POST');
}
// we do the following so no one can get clever
// permissions check
if (!$auth->acl_get('f_read', $print_post_forum))
{
	if ($user->data['user_id'] != ANONYMOUS)
	{
		trigger_error('SORRY_AUTH_READ');
	}

	login_box('', $user->lang['LOGIN_VIEWFORUM']);
}

if(!$auth->acl_get('f_print', $print_post_forum))
{
	trigger_error('SORRY_AUTH_PRINT');
}

	// Go ahead and pull all data for this postid
	$sql = 'SELECT p.*, t.topic_title, u.user_id, u.username, u.user_colour
		FROM ' . POSTS_TABLE . ' p
		LEFT JOIN '. TOPICS_TABLE . ' t ON p.topic_id = t.topic_id
		LEFT JOIN ' . USERS_TABLE . ' u ON p.poster_id = u.user_id
	       WHERE  p.post_id = ' . $print_post;

	$result = $db->sql_query_limit($sql, 1);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	// check for a result
	if (!$row['post_id'])
	{
		trigger_error('NO_POST');
	}
	// check to make sure the post is approved..if there is one
	if(!$row['post_approved'])
	{
		trigger_error('POST_UNAPPROVED');
 	}
 	// everything is okay...let's go
	$server_path = generate_board_url() . '/';
	$topic_title = censor_text($row['topic_title']);
	$poster_id = (int) $row['user_id'];
	$post_id = (int) $row['post_id'];

    $message = censor_text($row['post_text']);

	// initialize attachment stuffs
	$attachments = $attach_list = $update_count = array();
	$has_attachments = $display_notice = false;
	// Does the post have an attachment(s)? If so, add it/them
	if ($row['post_attachment'] && $config['allow_attachments'])
	{
		$attach_list[] = $post_id;
        $has_attachments = true;
	}
	// Pull attachment data
	if (sizeof($attach_list))
	{
		if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $print_post_forum))
		{
			$sql = 'SELECT *
				FROM ' . ATTACHMENTS_TABLE . '
				WHERE ' . $db->sql_in_set('post_msg_id', $attach_list) . '
					AND in_message = 0
				ORDER BY filetime DESC';
			$result = $db->sql_query($sql);

			while ($row2 = $db->sql_fetchrow($result))
			{
				$attachments[$row2['post_msg_id']][] = $row2;
			}
			$db->sql_freeresult($result);
		}
		else
		{
			$display_notice = true;
		}
	}
	// let's get us some attachments and show them...subsilver2 only though for now
	if (!empty($attachments[$row['post_id']]))
	{
		parse_attachments($print_post_forum, $message, $attachments[$row['post_id']], $update_count);
	}

	// initialize a needed variable for bbcode
	$bbcode_bitfield = '';
	// Define the global bbcode bitfield, will be used to load bbcodes
	$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
	// Instantiate BBCode if need be
	if ($bbcode_bitfield !== '')
	{
		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
	}
	// Second parse bbcode here
	if ($row['bbcode_bitfield'])
	{
		$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
	}
	// pretty up the message
	$message = bbcode_nl2br($message);
	$message = smiley_text($message);

	//generate the page
	$template->assign_vars(array(
		'MESSAGE'				=> $message,
		'TOPIC_TITLE'			=> $topic_title,
		'POST_DATE'				=> $user->format_date($row['post_time']),
		'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
		'POST_AUTHOR'			=> get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),

		'S_HAS_ATTACHMENTS'		=> (!empty($attachments[$row['post_id']])) ? true : false,
		'S_DISPLAY_NOTICE'		=> $display_notice && $row['post_attachment'],

		'U_FORUM'				=> $server_path,
		'U_POST'				=> "{$server_path}viewtopic.$phpEx?p=$post_id#p$post_id",

	));
	// Display Attachments for this post
	if (!empty($attachments[$row['post_id']]))
	{
		foreach ($attachments[$row['post_id']] as $attachment)
		{
			$template->assign_block_vars('attachment', array(
				'DISPLAY_ATTACHMENT'	=> $attachment)
			);
		}
	}
	unset($attachments[$row['post_id']]);


// Output the page
page_header($user->lang['PRINT_POST_VIEW']);
			
$template->set_filenames(array(
	'body' => 'viewtopic_print_post.html'
));
	
page_footer();

?>